<?php

class User_Dao{

	function __construct(){
	}
	
	//mendapatkan seluruh data user berupa array 
	function get_all(){
		$sql="
		select *
		from
		user
		";
		
		$list_user = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$user = new User();
				
				$user->id = $row['ID'];
				$user->email = $row['EMAIL'];
				$user->password = $row['PASSWORD'];
				$user->role = $row['ROLE'];
				
				$list_user[] = $user;
			}
		}	
		return $list_user;
	}
	
	//mendapatkan user berupa array berdasarkan parameter id
	function get_id($id){
		$sql="
		select *
		from
		user
		WHERE
		ID = '".$id."'
		";
		
		$user=false;		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
				
				$user = new User();
				
				$user->id = $row['ID'];
				$user->email = $row['EMAIL'];
				$user->password = $row['PASSWORD'];
				$user->role = $row['ROLE'];
				
			}
		}	
		return $user;
	}
	
	//menambahkan data user
	function add_user(User $user){
		$sql="
		insert 
		into 
		user
		values('',
		'$user->email',
		'$user->password',
		'$user->role')
		";
		$query=mysql_query($sql);
	}
	
	//mengubah data password user
	function edit_pass(User $user){
		$sql="
		update 
		user 
		set 
		PASSWORD='$user->password'
		where ID='$user->id'
		";
		$query=mysql_query($sql);
	}
	
	//mengubah data email user
	function edit_email(User $user){
		$sql="
		update 
		user 
		set 
		EMAIL='$user->email'
		where ID='$user->id'
		";
		$query=mysql_query($sql);
	}
	
	//mengecek input email dan password dan mengembalikan dalam bentuk boolean
	function cek_user(User $user){
		$sql="select count(*) as jml, EMAIL from user where EMAIL='$user->email' and PASSWORD='$user->password'";
		$query=mysql_query($sql);
		$data=mysql_fetch_assoc($query);
		$cek=false;
		//echo $data['jml'];
		if ($data['jml']=="1" && $data['EMAIL']==$user->email){
			$cek=true;
		}
		return $cek;
	}
	
	//mengecek input unique email dalam bentuk boolean
	function cek_email($email){
		$sql="select count(*) as jml from user where EMAIL='$email'";
		$query=mysql_query($sql);
		$data=mysql_fetch_assoc($query);
		$cek=false;
		//echo $data['jml'];
		if ($data['jml']==1){
			$cek=true;
		}
		return $cek;
	}
	
	//mendapatkan data user berupa array berdasarkan parameter email
	function get_ByEmail($email){
		$sql="
		select *
		from
		user
		WHERE
		EMAIL = '$email'
		";
		
		$user=false;		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
				$user = new User();
				
				$user->id = $row['ID'];
				$user->email = $row['EMAIL'];
				$user->password = $row['PASSWORD'];
				$user->role = $row['ROLE'];
			}
		}	
		return $user;
	}
	
	function randomPass(){

	$digit = 18;
	$karakter = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
	$i = 0;
	$pass = "";
	while ($i <= $digit-1){
		$num = rand() % 61;
		$tmp = substr($karakter,$num,1);
		$pass = $pass.$tmp;
		$i++;
	}
	return $pass;
	}
	
}


class User{
	var $id;
	var $email;
	var $password;
	var $role;
	

}